﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;

namespace bulkuploader_gui
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class addNewApp : Window
    {
        public addNewApp()
        {
            InitializeComponent();
        }

        private void addAppBtn_Click(object sender, RoutedEventArgs e)
        {
            Regex pattern = new Regex("^([a-zA-Z0-9-]{6,30})+$");
            if (pattern.IsMatch(textBox1.Text))
            {

                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load("appData.xml");
                }
                catch
                {
                    XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                    doc.AppendChild(dec);
                    XmlElement root = doc.CreateElement("apps");
                    doc.AppendChild(root);
                }
                if (doc.SelectSingleNode("//app[@name='" + textBox1.Text + "']") == null)
                {
                    XmlElement app = doc.CreateElement("app");
                    XmlElement name = doc.CreateElement("name");
                    XmlAttribute appName = doc.CreateAttribute("name");

                    name.InnerText = textBox1.Text;
                    app.AppendChild(name);
                    appName.InnerXml = textBox1.Text;
                    app.SetAttributeNode(appName);
                    doc.ChildNodes[1].AppendChild(app);
                    doc.Save("appData.xml");
                    if (Directory.Exists("backups") == false)
                        Directory.CreateDirectory("backups");
                    Directory.CreateDirectory("backups/" + textBox1.Text);
                    this.Close();
                }
                else
                    System.Media.SystemSounds.Beep.Play();
                
            }
            else
                System.Media.SystemSounds.Beep.Play();
        }
    }
}
